package com.kyzh.core.pager.weal.vip

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.VipCentreGift
import com.gushenge.core.impls.VipImpl
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemVipcentreContentHeaderBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.utils.setShader
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.fragment_vipcentre.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.textColor

class VipCentreHeardFragment : BaseFragment(){
    companion object{
        fun newInstance(type : Int): VipCentreHeardFragment {
            val args = Bundle().apply {
                putInt("type",type)
            }

            val fragment = VipCentreHeardFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_vipcentre, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
         type = arguments?.getInt("type")
        rvList.layoutManager = LinearLayoutManager(requireContext())
        heade.setVisibility(false)
        swipe.isRefreshing = false
        swipe.isEnabled = false
        rvList.adapter =  adapter
        adapter.setEmptyView(R.layout.empty)
        adapter.addChildClickViewIds(R.id.ad_full_id)
        getData1()
    }
    private var data1 :ArrayList<VipCentreGift>? = arrayListOf()
    private var type :Int? = 0
    private var type1 :Int = 0
    fun getData1(){
        VipImpl.getVipHeader(type){
            val arrayList = this
            arrayList?.apply {
                data1 = this
                adapter.setNewInstance(this)
            }

        }
    }

   val adapter =object:  BaseQuickAdapter<VipCentreGift,BaseDataBindingHolder<ItemVipcentreContentHeaderBinding>>(R.layout.item_vipcentre_content_header){
        override fun convert(
            holder: BaseDataBindingHolder<ItemVipcentreContentHeaderBinding>,
            item: VipCentreGift
        ) {
//            holder.setIsRecyclable(false)
            holder?.dataBinding?.data = item
            holder.dataBinding?.apply {
                if(item.name.isNullOrBlank()){
                    tvTab.setVisibility(false)
                }else{
                    tvTab.setVisibility(true)
                }
                when (item.is_peidai) {
                    0 -> {
                        content.setBackgroundResource(R.drawable.act_answer_submit_bg_red)
                        content.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#F3F4F8"))
                        tvAction.isEnabled = type != 2
                        tvAction.setVisibility(type != 2)
                        tvAction.text = "穿戴"
                        tvTips.text = item.title
                        if(type ==2){
                            tvTips.textSize = 14f
                        }
                        tvAction.textColor = Color.parseColor("#FF4F3200")
                        tvAction.backgroundResource = R.drawable.bg_btn_open
                    }
                    1 -> {
                        content.setBackgroundResource(R.drawable.bg_item_vip_select)
                        tvAction.isEnabled = type != 2
                        tvAction.setVisibility(type != 2)
                        if(type ==2){
                            tvTips.textSize = 14f
                            tvTips.text = "当前"
                            tvTips.textColor = Color.parseColor("#FFE7BD60")
                        }else{
                            tvTips.text = item.title
                            tvAction.textColor = Color.parseColor("#FF4F3200")
                        }

                        tvAction.text = "脱下"
                        tvAction.backgroundResource = R.drawable.bg_btn_opened
                    }
                    else -> {
                        content.setBackgroundResource(R.drawable.act_answer_submit_bg_red)
                        content.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#F3F4F8"))
                        tvAction.isEnabled = false
                        tvAction.setVisibility(type != 2)
                        tvTips.text = item.title
                         if(type ==2){
                            tvTips.textSize = 14f
                        }
                        tvAction.text = "穿戴"
                        tvAction.textColor = resources.getColor(R.color.font_66)
                        tvAction.backgroundResource = R.drawable.bg_btn_unopen
                    }
                }

                tvAction.setOnClickListener {
                    VipImpl.setVipHeaderItem(type,item.id){
                        var mindex = -1
                        data1?.forEachIndexed { index, it ->
                            Log.e("TAG", "data1: "+it.is_peidai )
                            if(it.is_peidai == 1){
                                data1?.get(index)?.is_peidai =0
                                Log.e("TAG", "--: "+it.is_peidai   + index)
                                mindex = index
                            }
                        }

                        if(holder.position ==mindex ){
                            notifyItemChanged(mindex)
                            // 发送同步 更换头像框   顶部及前一页
                            if(type ==1){
                                LiveEventBus.get<String>("frame_img").post("")
                            }
                            // 发送同步 更换勋章
                            if(type == 4){
                                LiveEventBus.get<String>("icon_img").post("")
                            }
                        }else {
                            notifyItemChanged(mindex)
                            data1?.get(holder.position)?.is_peidai =1
                            notifyItemChanged(holder.position)
                            // 发送同步 更换头像框   顶部及前一页
                            if(type ==1){
                                LiveEventBus.get<String>("frame_img").post(item.frame_img)
                            }
                            // 发送同步 更换勋章
                            if(type == 4){
                                LiveEventBus.get<String>("icon_img").post(item.medal_img)
                            }
                        }
                    }
                }
                tvName.post {
                    tvName.setShader(item.color)
                    tvName.setText(item.pet_name)
                }
            }
        }

    }
}